from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class Category(models.Model):
    """图书分类"""
    name = models.CharField(max_length=100, verbose_name='分类名称')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
    slug = models.SlugField(max_length=100, unique=True)
    description = models.TextField(blank=True)
    sort_order = models.IntegerField(default=0)
    is_active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'categories'
        verbose_name = '图书分类'
        verbose_name_plural = '图书分类'
        ordering = ['sort_order', 'name']

    def __str__(self):
        return self.name


class Tag(models.Model):
    """图书标签"""
    name = models.CharField(max_length=50, unique=True)
    color = models.CharField(max_length=7, default='#007bff')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'tags'
        verbose_name = '标签'
        verbose_name_plural = '标签'

    def __str__(self):
        return self.name


class Book(models.Model):
    """图书信息"""
    title = models.CharField(max_length=200, verbose_name='书名')
    author = models.CharField(max_length=100, verbose_name='作者')
    isbn = models.CharField(max_length=13, unique=True, verbose_name='ISBN')
    publisher = models.CharField(max_length=100, verbose_name='出版社')
    publication_date = models.DateField(verbose_name='出版日期')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    stock_quantity = models.IntegerField(default=0, verbose_name='库存数量')
    description = models.TextField(blank=True, verbose_name='图书简介')
    cover_image = models.ImageField(upload_to='books/covers/', blank=True, null=True, verbose_name='封面图片')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='books')
    tags = models.ManyToManyField(Tag, blank=True, related_name='books')
    pages = models.IntegerField(default=0, verbose_name='页数')
    language = models.CharField(max_length=20, default='中文', verbose_name='语言')
    weight = models.FloatField(default=0, verbose_name='重量(g)')
    dimensions = models.CharField(max_length=50, blank=True, verbose_name='尺寸')
    is_active = models.BooleanField(default=True, verbose_name='是否上架')
    is_featured = models.BooleanField(default=False, verbose_name='是否推荐')
    view_count = models.IntegerField(default=0, verbose_name='浏览次数')
    rating = models.FloatField(default=0.0, verbose_name='评分')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'books'
        verbose_name = '图书'
        verbose_name_plural = '图书'
        ordering = ['-created_at']

    def __str__(self):
        return self.title

    @property
    def is_in_stock(self):
        return self.stock_quantity > 0


class BookImage(models.Model):
    """图书附加图片"""
    book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='images')
    image = models.ImageField(upload_to='books/gallery/')
    alt_text = models.CharField(max_length=100, blank=True)
    sort_order = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'book_images'
        verbose_name = '图书图片'
        verbose_name_plural = '图书图片'
        ordering = ['sort_order']

    def __str__(self):
        return f"{self.book.title} 的图片"


class BookReview(models.Model):
    """图书评价统计"""
    book = models.OneToOneField(Book, on_delete=models.CASCADE, related_name='review_stats')
    total_reviews = models.IntegerField(default=0)
    average_rating = models.FloatField(default=0.0)
    five_star_count = models.IntegerField(default=0)
    four_star_count = models.IntegerField(default=0)
    three_star_count = models.IntegerField(default=0)
    two_star_count = models.IntegerField(default=0)
    one_star_count = models.IntegerField(default=0)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'book_reviews'
        verbose_name = '图书评价统计'
        verbose_name_plural = '图书评价统计'

    def __str__(self):
        return f"{self.book.title} 评价统计"
